package com.lk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.bean.Admin;
import com.lk.service.AdminService;
import com.lk.util.UploadFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 跳转管理员列表页面视图
     *
     * @return
     */
    @GetMapping("/goAdminListView")
    public String goAdminListView() {
        return "/admin/adminList";
    }

    /**
     * 分页获取admin数据
     *
     * @param page
     * @param rows
     * @param username
     * @return
     */
    @PostMapping("/getAdminList")
    @ResponseBody
    public Map<String, Object> getAdminList(Integer page, Integer rows, String username) {
        Map<String, Object> result = new HashMap<>();
        Admin admin = new Admin();
        admin.setName(username);
        //设置每页记录数
        PageHelper.startPage(page, rows);
        //获取指定的管理员信息，若没有username则获取全部admin数据
        List<Admin> list = adminService.getAdminList(admin);
        PageInfo<Admin> pageInfo = new PageInfo<>(list);

        //总页数
        long total = pageInfo.getTotal();

        result.put("total", total);
        result.put("rows", list);
        return result;
    }


    /**
     * 添加管理员
     *
     * @param admin
     * @return
     */
    @PostMapping("/addAdmin")
    @ResponseBody
    public Map<String, Object> addAdmin(Admin admin) {
        Map<String, Object> result = new HashMap<>();
        //请求参数是否为空
        if (admin == null) {
            result.put("success", false);
            result.put("msg", "添加数据不能为空");
            return result;
        }
        //插入数据的name是否已存在
        Admin admin1 = adminService.findByName(admin.getName());
        if (admin1 != null) {
            result.put("success", false);
            result.put("msg", "用户名已存在");
            return result;
        }

        //是否插入成功
        int row = adminService.addAdmin(admin);
        if (row == 1) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，添加失败");
        }
        return result;
    }


    /**
     * 修改管理员信息
     *
     * @param admin
     * @return
     */
    @PostMapping("/editAdmin")
    @ResponseBody
    public Map<String, Object> editAdmin(Admin admin) {
        Map<String, Object> result = new HashMap<>();
        //根据name查询是否已经存在同名的管理员
        Admin admin1 = adminService.findByName(admin.getName());
        //自己
        if (admin1 != null && admin1.getId() == admin.getId()) {
            int row = adminService.updateAdmin(admin);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("mag", "修改失败");
                return result;
            }
        } else if (admin1 != null && admin1.getId() != admin.getId()) { //名字存在但不是自己
            result.put("success", false);
            result.put("mag", "用户名已存在");
            return result;
        } else { //用户名不存在
            int row = adminService.updateAdmin(admin);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("mag", "修改失败");
                return result;
            }
        }
    }


    /**
     * （批量）根据id删除管理员
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteAdmin")
    @ResponseBody
    public Map<String, Object> deleteAdmin(@RequestParam(value = "ids[]") Integer[] ids) {
        Map<String, Object> result = new HashMap<>();

        int row = adminService.delete(ids);
        if (row > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "删除失败");
        }
        return result;
    }


    /**
     * 头像上传
     *
     * @return
     */
    @PostMapping("/uploadPhoto")
    @ResponseBody
    public Map<String, Object> uploadPhoto(MultipartFile photo, HttpServletRequest request) {
        //存储头像的本地目录
        final String realPath = request.getServletContext().getRealPath("/upload/admin_portrait/");
        //存储头像到项目发布目录
        final String portraitPath =
                request.getServletContext().getContextPath() + "/upload/admin_portrait/";
        //返回头像上传的结果
        return UploadFile.getUploadResult(photo, realPath, portraitPath);
    }


}
